function getLatency()
{
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onload=function()
    {
        latencyLine.append(new Date().getTime(), parseInt(this.responseText)/1000);
    }
    xmlhttp.open("GET","/latency",true);
    xmlhttp.send("");

}

function getThroughput()
{
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onload=function()
    {
        throughputLine.append(new Date().getTime(), parseInt(this.responseText)/(1024*1024));
    }
    xmlhttp.open("GET","/throughput",true);
    xmlhttp.send("");
}

function getDag()
{
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onload=function()
    {
        drawDag(this.responseXML);
    }
    xmlhttp.open("GET","/chain",true);
    xmlhttp.send("");
}

function drawDag(xmldag)
{
    var pluggins = xmldag.getElementsByTagName("plugin");
    for(var i = 0; i < pluggins.length; i++)
    {
        var net = "";
        var id = 0;
        var properties = pluggins[i].childNodes;
        for(var j = 0; j < properties.length; j++)
        {
            if(properties[j].nodeName.trim() === "net")
            {
                net = properties[j].firstChild.nodeValue;
            }
            if(properties[j].nodeName.trim() === "id")
            {
                id = properties[j].firstChild.nodeValue.trim();
            }
        }
        sigInst.addNode(id,{
                            label: net,
                            'x': Math.random()*0.8 + 0.1,
                            'y': Math.random()*0.8 + 0.1,
                            color: "#002eff"
                        });
    }
    var splitters = xmldag.getElementsByTagName("split");

    for(var i = 0; i < splitters.length; i++)
    {
        var net = "";
        var id = 0;
        var properties = splitters[i].childNodes;
        for(var j = 0; j < properties.length; j++)
        {
            if(properties[j].nodeName.trim() === "net")
            {
                net = properties[j].firstChild.nodeValue.trim();
            }
            if(properties[j].nodeName.trim() === "id")
            {
                id = properties[j].firstChild.nodeValue.trim();
            }
        }
        sigInst.addNode(id,{
                            label: net,
                            'x': Math.random()*0.8 + 0.1,
                            'y': Math.random()*0.8 + 0.1,
                            color: "#f00"
                        });
    }
    var queues = xmldag.getElementsByTagName("queue");
    for(var i = 0; i < queues.length; i++)
    {
        var net = "";
        var source = new Array();
        var sink = -1;
        var properties = queues[i].childNodes;
        for(var j = 0; j < properties.length; j++)
        {
            if(properties[j].nodeName.trim() === "net")
            {
                net = properties[j].firstChild.nodeValue.trim();
            }
            if(properties[j].nodeName.trim() === "sink")
            {
                sink = properties[j].firstChild.nodeValue.trim();
            }
            if(properties[j].nodeName.trim() === "source")
            {
                source.push(properties[j].firstChild.nodeValue.trim())
            }
        }
        if(source.length == 0 || sink == -1)
        {
            continue;
        }

        for(var j = 0; j < source.length; j++)
        {
            sigInst.addEdge(net + "" + j,source[j],sink);
        }

    }
    sigInst.startForceAtlas2();
}


var latencyPlot = new SmoothieChart();
var throughputPlot = new SmoothieChart();
latencyPlot.streamTo(document.getElementById("latency_cnv"), 1000);
throughputPlot.streamTo(document.getElementById("throughput_cnv"), 1000);

// Data
var latencyLine = new TimeSeries();
var throughputLine = new TimeSeries();

// Add a random value to each line every second
setInterval(function()
{
    getLatency();
    getThroughput();
}, 1000);

// Add to SmoothieChart
latencyPlot.addTimeSeries(latencyLine);
throughputPlot.addTimeSeries(throughputLine);

var sigRoot = document.getElementById('graph');
var sigInst = sigma.init(sigRoot);
sigInst.drawingProperties({
                              defaultLabelColor: '#fff',
                              defaultLabelSize: 14,
                              defaultLabelBGColor: '#fff',
                              defaultLabelHoverColor: '#000',
                              labelThreshold: 6,
                              defaultEdgeType: 'curve'
                          });
sigInst.graphProperties({
                            minNodeSize: 2,
                            maxNodeSize: 10,
                            minEdgeSize: 2,
                            maxEdgeSize: 3
                        });

sigInst.mouseProperties({maxRatio: 10});

getDag();
sigInst.draw();

var atlasTimer=setInterval(function()
{
    sigInst.stopForceAtlas2();
    clearInterval(atlasTimer);

},5000);

